const express = require("express");
const app = express();
const port = 9005;
app.use(express.json());
//jwt 身份令牌
const jwt = require("jsonwebtoken");
//getToken
const { getToken, deterToken, jxToken } = require("./util/index");
//cors
const cors = require("cors");
app.use(cors());

//mysql
// const { connection } = require("./util/mysql");
// const pool = require("./util/mysql");
const mysql = require("mysql");

//status
const { sus, errs } = require("./status/status");

//引入
const controlIndex = require("./control/index");
// app.use("/user", controlIndex);
app.use(controlIndex)
const userIndex = require("./user/index");
app.use(userIndex);
const handleIndex = require("./handle/index");
app.use(handleIndex);
const projectIndex = require("./project/index");
app.use(projectIndex);
const noticeIndex = require("./notice/index");
app.use(noticeIndex);
const dailyIndex = require("./daily/index");
app.use(dailyIndex);
const buyIndex=require('./buy/index')
app.use(buyIndex)

//第三方接口
const others=require('./others/index')
app.use(others)

//中间件
app.use(["/getInfo"], (req, res, next) => {
  let status = deterToken(req.headers.authorization);
  if (status == true) {
    next();
  } else {
    res.status(401).send({ code: 401, msg: "token过期" });
  }
});

const env = {
  host: "111.231.22.97",
  user: "wangs-web",
  password: "HsTjw82zF7f6fjWY",
  database: "wangs-web", //数据库名字
};

var timer;
const connect = () => {
  //每小时连接一次
  timer = setInterval(() => {
    let connection = mysql.createConnection(env);
    connection.connect();
  }, 3600000);
};
connect();

//登录操作
app.post("/login", (req, res) => {
  console.log(req.body);
  var pools = mysql.createPool(env);
  pools.getConnection(function (err, connection) {
    if (err) {
      res.status(500);
      res.send({
        code: 500,
        msg: "error",
      });
    } else {
      connection.query(
        "select * from userlist where username=? and password=?",
        [req.body.username, req.body.password],
        (err, result) => {
          if (err) {
            console.log(err, "err");
            res.status(500);
            res.send({
              code: 500,
              msg: "error",
            });
          } else {
            console.log(result, "9999");
            if (result[0] && result.length == 1) {
              let token = getToken(req.body);
              res.send({
                code: 200,
                msg: "success",
                data: {
                  token: token,
                },
              });
            } else {
              res.send(errs("登录失败"));
            }
          }
        }
      );
    }
    connection.release();
  });
});

app.get("/getInfo", (req, res) => {
  var pools = mysql.createPool(env);
  console.log(req.query,'11111')
  pools.getConnection(function (err, connection) {
    if (err) throw err;
    let obj = jxToken(req.query.token);
    console.log(obj, "323");
    connection.query(
      "select * from userlist where username=?",
      [obj.username],
      (err, result) => {
        if (err) {
          res.status(500);
          res.send(errs("error"));
        } else {
          console.log(result, "000");
          res.send({
            code: 200,
            data: {
              username: result[0].username,
              name: result[0].name,
              avatar:
                "https://img0.baidu.com/it/u=1739463261,948605086&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1724691600&t=8e7d1e993b0523ecec5869db0485dcff",
              role: result[0].role,
              id: result[0].id,
            },
          });
          connection.release();
        }
      }
    );
  });
});

app.listen(port, () => {
  console.log("app is now start");
});
